/* eslint-disable newline-per-chained-call */
import {
  autoInputAccount, deleteBox, asyncSetInterval, baoBiaoDetailBack, ZDYpostMessage
} from './utils.js';

let pages = {
  sjIndex: 'https://www.etax-gd.gov.cn/xxmh/html/index.html',
  loginIndex: 'https://www.etax-gd.gov.cn/sso/login',
  index: 'https://www.etax-gd.gov.cn/xxmh/html/index_login.html',
  ysbIndex: 'https://www.etax-gd.gov.cn/xxmh/html/index_origin.html',
  ybnsrzzs: 'https://www.etax-gd.gov.cn/sbzx-cjpt-web/biz/sb/ybnsrzzs'
};
let ispage = (page, isQuery) => {
  let href = window.location.href;
  if (isQuery) return href.includes(pages[page]);
  return href === pages[page];
};

let loginIndex = async () => {
  document.querySelector('body > div:nth-child(1) > div.indextop > div.layui-row > div.layui-col-xs5.rcon > div.dlbox > a').click();
};

// index 页面
let indexOption = async () => {
  // 删除提示框
  deleteBox();
  await asyncSetInterval(() => {
    let wybs = $('#wybs');
    if (wybs.length) {
      wybs.click();
      setTimeout(() => {
        let list = $('#topTabs > div.layui-tab-content > div')[1];
        if (list) {
          let alist = $(list).find('.itme-icon a');
          let clist = Array.from(alist).find(item => $(item).text() === '税费申报及缴纳');
          clist.click();
        }
      }, 500);
      return true;
      // let btn = Array.from(lis).find(item => $(item).text() === '申报缴纳');
      // if (btn) btn.click();
    }
  }, 1000);
};
// 应申报页面
let ysbOption = async (data) => {
  let { year, endPeriod } = data;
  // 增值税申报
  await asyncSetInterval(() => {
    let ifrMain = $('#ifrMain');
    if (ifrMain.length && ifrMain.contents().find('#lhsbIframe').length) {
      let lhsbIframe = ifrMain.contents().find('#lhsbIframe');
      if (lhsbIframe.contents().find('#gdslhsb > tbody > tr').length > 1) {
        let trs = lhsbIframe.contents().find('#gdslhsb > tbody > tr');
        let trs1 = lhsbIframe.contents().find('#viewCtrlid > div:nth-child(6) > div > table > tbody > tr');
        // 剔除不是当期的税种?
        let arr = [...Array.from(trs), ...Array.from(trs1)];
        arr = arr.filter(item => {
          if ($(item).find('th:nth-child(1)').text() === '序号') return false;
          let kssj = $(item).find('td:nth-child(4)').text().split('-');
          let jssj = $(item).find('td:nth-child(5)').text().split('-');
          let declareCycle = parseInt(jssj[1]) - parseInt(kssj[1]);
          if (declareCycle === 11) return true;
          return parseInt(jssj[1]) === endPeriod;
        });
        // 增值税一般纳税人 增值税(适用于一般纳税人)
        let zzsybnsr = arr.find(item => $(item).find('td:nth-child(8)').text().includes('填写申报表') && $(item).find('td:nth-child(2)').text().includes('增值税(适用于一般纳税人)'));
        if (zzsybnsr) {
          zzsybnsrBtn(zzsybnsr, ifrMain);
          return -1;
        }
        return arr;
      }
    }
  }).then(arr => {
    if (arr !== -1) {
      let recodeList = arr.map(item => {
        let kssj = $(item).find('td:nth-child(4)').text().split('-');
        let jssj = $(item).find('td:nth-child(5)').text().split('-');
        let declareCycle = parseInt(jssj[1]) - parseInt(kssj[1]);
        let declareName = $(item).find('td:nth-child(2)').text();
        let isSuccess = false;
        let obj = {
          declareName,
          accountBook: declareCycle === 11 ? jssj[1] : (`${year}-${endPeriod > 9 ? endPeriod : ('0' + endPeriod)}`),
          declareCycle: declareCycle === 2 ? 'season' : (declareCycle === 11 ? 'year' : 'month')
        };
        if (declareName.includes('企业会计')) {
          isSuccess = $(item).find('td:nth-child(7)').text() === '已报送';
        } else {
          isSuccess = $(item).find('td:nth-child(8)').text() === '已申报' && $(item).find('td:nth-child(8)').text() === '已申报';
        }
        obj.isSuccess = isSuccess;
        return obj;
      });
      return recodeList;
    }
    return -1;
  }).then(recodeList => {
    if (recodeList !== -1) {
      let backInfo = {
        ...data,
        recordList: recodeList
      };
      ZDYpostMessage({
        event: 'zeroReportBack',
        data: backInfo
      });
    }
  });
};
let ybnsrzzsOption = async (data) => {
  setTimeout(async () => {
    deleteBox($('.layui-layer.layui-layer-dialog > span.layui-layer-setwin > a'));
    $('#btnSave').click();
    await asyncSetInterval(() => {
      if ($('#frmMain').length && $('#frmMain').contents().find('#viewCtrlId > div.NewTableHead > h1').length) {
        if ($('#frmMain').contents().find('#viewCtrlId > div.NewTableHead > h1').text() === '增值税一般纳税人报表选择') {
          $('#btnSave').click();
          return true;
        }
      }
    }, 2000).then(() => {
      return asyncSetInterval(() => {
        let btn = $('.layui-layer.layui-layer-dialog > div.layui-layer-btn.layui-layer-btn- > a');
        if (btn) {
          btn.click();
          return true;
        }
      }, 500);
    }).then(() => {
      return asyncSetInterval(() => {
        if ($('#frmMain').length && $('#frmMain').contents().find('#frmSheet').length && $('#frmMain').contents().find('#frmSheet').contents().find('#viewCtrlId > div.NewTableHead > h1').length) {
          if ($('#frmMain').contents().find('#frmSheet').contents().find('#viewCtrlId > div.NewTableHead > h1').text()
            .includes('增值税纳税申报表')) {
            return asyncSetInterval(() => {
              deleteBox($('.layui-layer.layui-layer-dialog > span.layui-layer-setwin > a'));
              let closeTsjjdjxx = $('#frmMain').contents().find('#closeTsjjdjxx');
              if (closeTsjjdjxx) closeTsjjdjxx[0].click();
              return true;
            }, 2000);
          }
        }
      }, 200);
    }).then(() => {
      return asyncSetInterval(() => {
        $('#btnPrepareMake').click();
        return true;
      }, 1000);
    }).then(() => {
      return asyncSetInterval(() => {
        if ($('#frmMain').length && $('#frmMain').contents().find('.layui-layer.layui-layer-dialog > div.layui-layer-btn.layui-layer-btn- > a.layui-layer-btn0').length) {
          $('#frmMain').contents().find('.layui-layer.layui-layer-dialog > div.layui-layer-btn.layui-layer-btn- > a.layui-layer-btn0')[0].click();
          return true;
        }
      }, 1000);
    }).then(() => {
      // 倒计时
      return asyncSetInterval(() => {
        return true;
      }, 5000);
    }).then(() => {
      // 申报成功
      return asyncSetInterval(() => {
        if ($('#frmMain').length && $('#frmMain').contents().find('body > div > h3').length) {
          if ($('#frmMain').contents().find('body > div > h3').text() === '申报成功') {
            baoBiaoDetailBack('zzsybnsr', true, 1000);
          }
          return true;
        }
      }, 1000);
    });
  }, 1000);
};


// 增值税一般纳税人按钮点击
const zzsybnsrBtn = (zzsybnsr, ifrMain) => {
  if (zzsybnsr) $(zzsybnsr).find('td:nth-child(8) a')[0].click();
  asyncSetInterval(() => {
    let trs = ifrMain.contents().find('.layui-layer.layui-anim.layui-layer-page  > div.layui-layer-content > div > table > tbody > tr');
    if (trs.length) {
      let online = Array.from(trs).find(item => $(item).find('td:nth-child(1)').text().includes('在线申报'));
      if (online) $(online).find('td:nth-child(2) button')[0].click();
      return true;
    }
  }, 1000);
};


const zeroReport = async (data) => {
  if (ispage('loginIndex', true)) autoInputAccount(data);
  else if (ispage('sjIndex')) loginIndex();
  else if (ispage('index', true)) indexOption();
  else if (ispage('ysbIndex', true)) ysbOption(data);
  else if (ispage('ybnsrzzs', true)) ybnsrzzsOption(data);
  else if (ispage('ybnsrzzs', true)) ybnsrzzsOption(data);
};


export {
  zeroReport
};
